#include "StdAfx.h"
#include "CTraitement.h"
#include "CGrabber.h"
#include "math.h"
#include "resource.h"

CTraitement::CTraitement(void)
{

}

void CTraitement::AfficheCroix(CGrabber* im, int x, int y, int nb, BYTE Intensite)
{
	for(int i = 0; i <= nb; i++)
	{
		im->SetPixel((x - nb / 2) + i, y, Intensite);
		im->SetPixel(x, (y - nb/2) + i, Intensite);
	}
}

void CTraitement::Seuillage(CGrabber* Im, int Seuil)
{
	for(int x = 0; x< Im->Width; x++)
	{
		for(int y = 0; y<Im->Height; y++)
		{
			if(Im->GetPixel(x, y) < Seuil)
				Im->SetPixel(x, y, 0);
			else
				Im->SetPixel(x, y, 255);
		}
	}
}

void CTraitement::Contour(CGrabber* Im, HWND hDlg, int g_min)
{
	for(int y = 0; y < 480; y++)
		{
			for(int x = 0; x < 640; x++)
			{
				if(Im->GetPixel(x,y) < g_min  && Im->GetPixel(x, y) != CouleurContour)
				{
					if(Im->GetPixel(x-1,y) > g_min  && Im->GetPixel(x-1, y) != CouleurContour)
						//Im->SetPixel(x-1,y,255);
						SetPixelGrosseur(Im, x-1, y, CouleurContour, 1);
					else
						if(Im->GetPixel(x+1,y) > g_min && Im->GetPixel(x+1, y) != CouleurContour)
							//Im->SetPixel(x+1,y,255);
							SetPixelGrosseur(Im, x+1, y, CouleurContour, 1);

					if(Im->GetPixel(x,y-1) > g_min && Im->GetPixel(x, y-1) != CouleurContour)
						//Im->SetPixel(x,y-1,255);
						SetPixelGrosseur(Im, x, y-1, CouleurContour, 1);
					else
						if(Im->GetPixel(x,y+1) > g_min && Im->GetPixel(x, y+1) != CouleurContour)
							//Im->SetPixel(x,y+1,255);
							SetPixelGrosseur(Im, x, y+1, CouleurContour, 1);

				}
			}
		}

	Im->Draw(GetDC(hDlg),640,480,0,0,640,480);
}

int CTraitement::CalculAngle(CGrabber* Im, HWND hDlg, int g_min)
{
	POINT minx;
	POINT miny;
	POINT maxx;
	POINT maxy;

	minx.x = 640;
	miny.y = 480;
	maxx.x = 0;
	maxy.y = 0;

	int Angle;

	for(int y = 0; y < 480; y++)
	{
		for(int x = 0; x < 640; x++)
		{
			if(Im->GetPixel(x,y) < g_min)
			{
				if(x < minx.x)
				{
					minx.x = x;
					minx.y = y;
				}
				if(x >= maxx.x)
				{
					maxx.x = x;
					maxx.y = y;
				}
				if(y < miny.y)
				{
					miny.y = y;
					miny.x = x;
				}
				if(y >= maxy.y)
				{
					maxy.y = y;
					maxy.x = x;
				}

			}
		}
	}
	
	if(abs(minx.x - miny.x) < 10 || abs(maxx.x - maxy.x) < 10 || abs(maxx.x - miny.x) < 10 || abs(minx.x - maxy.x) < 10)
	{
		AfficheCroix(Im,minx.x,miny.y,20,255);
		AfficheCroix(Im,minx.x,maxy.y,20,255);
		AfficheCroix(Im,maxx.x,miny.y,20,255);
		AfficheCroix(Im,maxx.x,maxy.y,20,255);

		Angle = 0;
	}
	else
	{
		AfficheCroix(Im,maxx.x,maxx.y,20,255);
		AfficheCroix(Im,maxy.x,maxy.y,20,255);
		AfficheCroix(Im,minx.x,minx.y,20,255);
		AfficheCroix(Im,miny.x,miny.y,20,255);

		Angle = (int)(atan2((double)(maxy.y - maxx.y),(double)(maxx.x - maxy.x)) * 180 / 3.1416);
	}

	AfficheCroix(Im,(minx.x + maxx.x) / 2,(miny.y + maxy.y) / 2,20,255);	

	return Angle;
}

int CTraitement::SeuillageAuto(HWND hDlg, int x1, int x2, LPPOINT LinePoints)
{
	int low = 255;
	int high = 0;
	int Mediane = 0;
	HWND hPC = GetDlgItem(hDlg, IDC_GRAPH);

	for(int x = 0; x < x2 - x1; x++)
	{
		if(260 - LinePoints[x].y > high)
			high = 260 - LinePoints[x].y;
		if(260 - LinePoints[x].y < low)
			low = 260-LinePoints[x].y;
	}			

	Mediane = (high + low) / 2;
	Polyline(GetDC(hPC), LinePoints, x2 - x1);
	SetDlgItemInt(hDlg,IDC_SEUIL,Mediane,0);
	SetDlgItemInt(hDlg,IDC_MIN,low,0);
	SetDlgItemInt(hDlg,IDC_MAX,high,0);

	POINT Lin[2];
	Lin[0].x = 50;
	Lin[1].x = 700;
	Lin[0].y = 260 - Mediane;
	Lin[1].y = 260 - Mediane;

	Polyline(GetDC(hPC), Lin, 2);

	return Mediane;
}

void CTraitement::SetPixelGrosseur(CGrabber* Im, int x, int y, int Couleur, int Taille)
{
	for(int i = y-Taille; i<=y+Taille; i++)
	{
		for(int j = x-Taille; j<=x+Taille; j++)
			Im->SetPixel(j, i, Couleur);
	}
}

CTraitement::~CTraitement(void)
{
}
